package com.jzg.mvc.biz.cab;

import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jzg.common.model.biz.Cab;
import com.jzg.common.model.biz.CabCard;
import com.jzg.common.model.biz.CabModule;
import com.jzg.common.model.biz.CabModuleBoard;
import com.jzg.common.model.biz.CabModuleFaultBox;
import com.jzg.common.model.biz.CabStore;
import com.jzg.common.model.biz.CabStoreBox;
import com.jzg.common.model.sys.SysOrg;
import com.jzg.common.render.RenderLayuiBean;


public class CabService{
	
	public RenderLayuiBean listData(CabController cabController) {
		int pageNumber = cabController.getParaToInt("page", 1);
		int pageSize = cabController.getParaToInt("limit", 10);
		String keyword = cabController.getPara("keyword");
		
		String sqlExceptSelect=" FROM "+Cab.TABLE_NAME+"  C,"+SysOrg.TABLE_NAME+" O  WHERE 1=1 AND C.ORGID=O.ID ";
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " AND O.NAME like '%" + keyword + "%' ";
		}
		Page<Cab> page = Cab.dao.paginate(pageNumber, pageSize, "SELECT C.*,O.NAME ", sqlExceptSelect);
		List<Cab> cabs = page.getList();
		if(cabs!= null && cabs.size()>0) {
			for (Cab cab : cabs) {
				int used = Db.queryInt("select count(*) from "+CabStoreBox.TABLE_NAME+" where cabId=?",cab.getId());
				int fault = Db.queryInt("select count(*) from "+CabModuleFaultBox.TABLE_NAME+" where status='0' and cabId=?",cab.getId());
				int total = 0;
				Record record = Db.findFirst("select SUM(total) total from biz_cab_module where cabId=? ",cab.getId());
				if(!StrKit.isBlank(record.getStr("total"))) {
					total = record.getInt("total");
				}
				
				cab.setFaultNum(fault);
				cab.setUsedNum(used);
				cab.setFreeNum(total-fault-used);
				cab.setTotalNum(total);
				
				cabCaseHandle(cab);
			}
		}
		RenderLayuiBean renderBean=new RenderLayuiBean();
		renderBean.setCode(0);
		renderBean.setMsg("查询成功");
		renderBean.setData(page.getList());
		renderBean.setCount(page.getTotalRow());
		return renderBean ;
	}
	
	public void delete (Cab cab) {
		Db.delete("delete from "+CabCard.TABLE_NAME+" where cabId=? ",cab.getId());
		List<CabModule> cabModules = CabModule.dao.find("select * from "+CabModule.TABLE_NAME+" where cabId=? ",cab.getId());
		if(cabModules!=null && cabModules.size()>0) {
			for (CabModule cabModule : cabModules) {
				if(cabModule.delete()) {
					Db.delete("delete from "+CabModuleBoard.TABLE_NAME+" where moduleId=? ",cabModule.getId());
				}
			}
		}
	}
	
	/**
	 * 柜子案件状态统计
	 * @param cab
	 */
	public void cabCaseHandle(Cab cab) {
		int caseNum=0;//总数
		int normalNum=0;//正常2
		int overdueNum=0;//超期-1
		int warnNum=0;//预警0
		int trialNum=0;//待审1
		int completeNum=0;//完成3
		SysOrg org = SysOrg.dao.findById(cab.getOrgId());
		if(org!=null) {
			List<CabStore>  stores = CabStore.dao.find("select * from "+CabStore.TABLE_NAME+" where cabId=? ",cab.getId());
			if(stores!=null && stores.size()>0) {
				caseNum = stores.size();
				for (CabStore cabStore : stores) {
					if(StrKit.equals("-1", cabStore.getStatus())) {
						overdueNum ++;
					}else if(StrKit.equals("0", cabStore.getStatus())) {
						warnNum ++;
					}else if(StrKit.equals("1", cabStore.getStatus())) {
						trialNum ++;
					}else if(StrKit.equals("2", cabStore.getStatus())) {
						normalNum ++;
					}else if(StrKit.equals("3", cabStore.getStatus())){
						completeNum ++;
					}else {
						if(StrKit.equals("53010305", org.getCODE())) {
							trialNum ++;
						}else {
							normalNum ++;
						}
					}
				}
			}
			if(StrKit.equals("53010305", org.getCODE())) {
				cab.setWaitingNum(trialNum);
			}else {
				cab.setNormalNum(normalNum);
			}
		}
		cab.setWarningNum(warnNum);
		cab.setCaseNum(caseNum);
		cab.setOverdueNum(overdueNum);
		cab.setFinishNum(completeNum);
	}
	
}
